package com.keelau.jxbrowser;

import com.keelau.ui.fx.ComponentUtils;
import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.*;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;

import static java.awt.Color.*;

/**
 * The sample demonstrates how to register custom ContextMenuHandler,
 * to handle mouse right clicks and display custom JavaFX context menu.
 */
public class JavaFXContextMenuSample extends Application {
    @Override
    public void start(Stage primaryStage) {
        //LoggerProvider.setLevel(Level.ALL);
        Browser browser = new Browser();

        BrowserView browserView = new BrowserView(browser);
        browserView.setMouseTransparent(true);

        browserView.setMouseEventsHandler(new InputEventsHandler<MouseEvent>() {
            @Override
            public boolean handle(MouseEvent event) {
                //LoggerFactory.getLogger(JavaFXContextMenuSample.class).info("setMouseEventsHandler");
                return false;
            }
        });
        browserView.setOnMouseClicked(event -> {
            LoggerFactory.getLogger(JavaFXContextMenuSample.class).info("setOnMouseClicked");
        });

        com.teamdev.jxbrowser.chromium.javafx.internal.Widget widget=  ( com.teamdev.jxbrowser.chromium.javafx.internal.Widget) browserView.getChildren().get(0);

        widget.setBorder(ComponentUtils.getBorder(null));
        widget.setOnMouseClicked(event -> {
            LoggerFactory.getLogger(JavaFXContextMenuSample.class).info("widget mouse click");

        });


        browser.setContextMenuHandler(new MyContextMenuHandler(browserView));

        StackPane pane = new StackPane();
        pane.getChildren().add(browserView);
        Scene scene = new Scene(pane, 700, 500);
        primaryStage.setScene(scene);
        primaryStage.show();

        browser.loadURL("http://www.baidu.com");
    }

    public static void main(String[] args) {
        launch(args);
    }

    private static class MyContextMenuHandler implements ContextMenuHandler {

        private final Pane pane;

        private MyContextMenuHandler(Pane paren) {
            this.pane = paren;
        }

        public void showContextMenu(final ContextMenuParams params) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    createAndDisplayContextMenu(params);
                }
            });
        }

        private void createAndDisplayContextMenu(final ContextMenuParams params) {
            final ContextMenu contextMenu = new ContextMenu();

            // Since context menu doesn't auto hide, listen mouse press events
            // on BrowserView and hide context menu on mouse press
            pane.getChildren().get(0).setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    contextMenu.hide();
                }
            });

            contextMenu.addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, event -> {
                contextMenu.hide();
                LoggerFactory.getLogger(JavaFXContextMenuSample.class).info("mouse exit");
            });



            pane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    contextMenu.hide();
                }
            });

            // If there's link under mouse pointer, create and add
            // the "Open link in new window" menu item to our context menu
            if (!params.getLinkText().isEmpty()) {
                contextMenu.getItems().add(createMenuItem(
                        "Open link in new window", new Runnable() {
                            public void run() {
                                String linkURL = params.getLinkURL();
                                System.out.println("linkURL = " + linkURL);
                            }
                        }));
            }

            // Create and add "Reload" menu item to our context menu
            contextMenu.getItems().add(createMenuItem("Reload", new Runnable() {
                public void run() {
                    params.getBrowser().reload();
                }
            }));

            // Display context menu at required location on screen
            Point location = params.getLocation();
            Point2D screenLocation = pane.localToScreen(location.x, location.y);
            LoggerFactory.getLogger(JavaFXContextMenuSample.class).info("x:"+location.x + ";y:" +location.y );
            contextMenu.show(pane.getChildren().get(0), screenLocation.getX(), screenLocation.getY());
        }

        private static MenuItem createMenuItem(String title, final Runnable action) {
            MenuItem menuItem = new MenuItem(title);
            menuItem.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    action.run();
                }
            });
            return menuItem;
        }
    }
}